home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / misc / gs261src.zip / zdps1.c < prev    next >
C/C++ Source or Header  |  1993-05-13  |  10KB  |  374 lines

  1. /* Copyright (C) 1990, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* zdps1.c */
  20. /* Display PostScript graphics extensions */
  21. #include "ghost.h"
  22. #include "errors.h"
  23. #include "oper.h"
  24. #include "gsmatrix.h"
  25. #include "gspath.h"
  26. #include "gsstate.h"
  27. #include "alloc.h"
  28. #include "ivmspace.h"
  29. #include "state.h"
  30. #include "store.h"
  31. #include "stream.h"
  32. #include "bnum.h"
  33.  
  34. /* Imported data */
  35. extern op_proc_p zcopy_procs[t_next_index];
  36.  
  37. /* Forward references */
  38. private int zcopy_gstate(P1(os_ptr));
  39.  
  40. /* Initialize by adding an entry for gstates to the `copy' operator. */
  41. private void
  42. zdps1_init(void)
  43. {    zcopy_procs[t_gstate] = zcopy_gstate;
  44. }
  45.  
  46. /* ------ Graphics state ------ */
  47.  
  48. /* <bool> setstrokeadjust - */
  49. int
  50. zsetstrokeadjust(register os_ptr op)
  51. {    check_type(*op, t_boolean);
  52.     gs_setstrokeadjust(igs, op->value.index);
  53.     pop(1);
  54.     return 0;
  55. }
  56.  
  57. /* - currentstrokeadjust <bool> */
  58. int
  59. zcurrentstrokeadjust(register os_ptr op)
  60. {    push(1);
  61.     make_bool(op, gs_currentstrokeadjust(igs));
  62.     return 0;
  63. }
  64.  
  65. /* <x> <y> sethalftonephase - */
  66. int
  67. zsethalftonephase(register os_ptr op)
  68. {    int code;
  69.     long x, y;
  70.     check_type(op[-1], t_integer);
  71.     check_type(*op, t_integer);
  72.     x = op[-1].value.intval;
  73.     y = op->value.intval;
  74.     if ( x != (int)x || y != (int)y )
  75.         return_error(e_rangecheck);
  76.     code = gs_sethalftonephase(igs, (int)x, (int)y);
  77.     if ( code >= 0 ) pop(2);
  78.     return code;
  79. }
  80.  
  81. /* - currenthalftonephase <x> <y> */
  82. int
  83. zcurrenthalftonephase(register os_ptr op)
  84. {    gs_int_point phase;
  85.     gs_currenthalftonephase(igs, &phase);
  86.     push(2);
  87.     make_int(op - 1, phase.x);
  88.     make_int(op, phase.y);
  89.     return 0;
  90. }
  91.  
  92. /* ------ Graphics state objects ------ */
  93.  
  94. /* Check to make sure that all the elements of a graphics state */
  95. /* are global.  ****** DOESN'T CHECK THE NON-REFS. ****** */
  96. private int
  97. gstate_check_global(int_gstate *isp)
  98. {
  99. #define gsref_check(p) check_global(*(p))
  100.     int_gstate_map_refs(isp, gsref_check);
  101. #undef gsref_check
  102.     return 0;
  103. }
  104.  
  105. /* - gstate <gstate> */
  106. int
  107. zgstate(register os_ptr op)
  108. {    gs_state *pnew;
  109.     int_gstate *isp;
  110.     if ( !alloc_current_local() )
  111.     {    int code = gstate_check_global(&istate);
  112.         if ( code < 0 ) return code;
  113.     }
  114.     pnew = gs_gstate(igs);
  115.     if ( pnew == 0 )
  116.         return_error(e_VMerror);
  117.     isp = (int_gstate *)gs_state_client_data(pnew);
  118.     int_gstate_map_refs(isp, ref_mark_new);
  119.     push(1);
  120.     make_tv(op, t_gstate, pgstate, pnew);
  121.     return 0;
  122. }
  123.  
  124. /* copy for gstates */
  125. private int
  126. zcopy_gstate(register os_ptr op)
  127. {    os_ptr op1 = op - 1;
  128.     gs_state *pgs;
  129.     gs_state *pgs1;
  130.     int_gstate *pistate;
  131.     int code;
  132.     check_type(*op, t_gstate);
  133.     check_type(*op1, t_gstate);
  134.     pgs = op->value.pgstate;
  135.     pgs1 = op1->value.pgstate;
  136.     pistate = (int_gstate *)gs_state_client_data(pgs);
  137.     if ( !r_is_global(op) )
  138.     {    code = gstate_check_global((int_gstate *)gs_state_client_data(pgs1));
  139.         if ( code < 0 ) return code;
  140.     }
  141. #define gsref_save(p) ref_save(p, "currentgstate")
  142.     int_gstate_map_refs(pistate, gsref_save);
  143. #undef gsref_save
  144.     /****** DOESN'T GET FULLY UNDONE BY RESTORE ******/
  145.     code = gs_copygstate(pgs, pgs1);
  146.     if ( code < 0 ) return code;
  147.     int_gstate_map_refs(pistate, ref_mark_new);
  148.     *op1 = *op;
  149.     pop(1);
  150.     return 0;
  151. }
  152.  
  153. /* <gstate> currentgstate <gstate> */
  154. int
  155. zcurrentgstate(register os_ptr op)
  156. {    int code;
  157.     int_gstate *pistate;
  158.     check_type(*op, t_gstate);
  159.     pistate = (int_gstate *)gs_state_client_data(op->value.pgstate);
  160.     if ( !r_is_global(op) )
  161.     {    code = gstate_check_global(&istate);
  162.         if ( code < 0 ) return code;
  163.     }
  164. #define gsref_save(p) ref_save(p, "currentgstate")
  165.     int_gstate_map_refs(pistate, gsref_save);
  166. #undef gsref_save
  167.     /****** DOESN'T GET FULLY UNDONE BY RESTORE ******/
  168.     code = gs_currentgstate(op->value.pgstate, igs);
  169.     if ( code < 0 ) return code;
  170.     int_gstate_map_refs(pistate, ref_mark_new);
  171.     return 0;
  172. }
  173.  
  174. /* <gstate> setgstate - */
  175. int
  176. zsetgstate(register os_ptr op)
  177. {    int code;
  178.     check_type(*op, t_gstate);
  179.     code = gs_setgstate(igs, op->value.pgstate);
  180.     if ( code < 0 ) return code;
  181.     pop(1);
  182.     return 0;
  183. }
  184.  
  185. /* ------ Rectangles ------- */
  186.  
  187. /* We preallocate a short list for rectangles, because */
  188. /* the rectangle operators usually will involve very few rectangles. */
  189. #define max_local_rect 5
  190. typedef struct local_rects_s {
  191.     gs_rect *pr;
  192.     uint count;
  193.     gs_rect rl[max_local_rect];
  194. } local_rects;
  195.  
  196. /* Forward references */
  197. private int rect_get(P2(local_rects *, os_ptr));
  198. private void rect_release(P1(local_rects *));
  199.  
  200. /* <x> <y> <width> <height> rectappend - */
  201. /* <numarray|numstring> rectappend - */
  202. int
  203. zrectappend(os_ptr op)
  204. {    local_rects lr;
  205.     int npop = rect_get(&lr, op);
  206.     int code;
  207.     if ( npop < 0 ) return npop;
  208.     code = gs_rectappend(igs, lr.pr, lr.count);
  209.     rect_release(&lr);
  210.     if ( code < 0 ) return code;
  211.     pop(npop);
  212.     return 0;
  213. }
  214.  
  215. /* <x> <y> <width> <height> rectclip - */
  216. /* <numarray|numstring> rectclip - */
  217. int
  218. zrectclip(os_ptr op)
  219. {    local_rects lr;
  220.     int npop = rect_get(&lr, op);
  221.     int code;
  222.     if ( npop < 0 ) return npop;
  223.     code = gs_rectclip(igs, lr.pr, lr.count);
  224.     rect_release(&lr);
  225.     if ( code < 0 ) return code;
  226.     pop(npop);
  227.     return 0;
  228. }
  229.  
  230. /* <x> <y> <width> <height> rectfill - */
  231. /* <numarray|numstring> rectfill - */
  232. int
  233. zrectfill(os_ptr op)
  234. {    local_rects lr;
  235.     int npop = rect_get(&lr, op);
  236.     int code;
  237.     if ( npop < 0 ) return npop;
  238.     code = gs_rectfill(igs, lr.pr, lr.count);
  239.     rect_release(&lr);
  240.     if ( code < 0 ) return code;
  241.     pop(npop);
  242.     return 0;
  243. }
  244.  
  245. /* <x> <y> <width> <height> rectstroke - */
  246. /* <numarray|numstring> rectstroke - */
  247. int
  248. zrectstroke(os_ptr op)
  249. {    gs_matrix mat;
  250.     local_rects lr;
  251.     int npop, code;
  252.     if ( read_matrix(op, &mat) >= 0 )
  253.        {    /* Concatenate the matrix to the CTM just before */
  254.         /* stroking the path. */
  255.         npop = rect_get(&lr, op - 1);
  256.         if ( npop < 0 ) return npop;
  257.         code = gs_rectstroke(igs, lr.pr, lr.count, &mat);
  258.         npop++;
  259.        }
  260.     else
  261.        {    /* No matrix. */
  262.         npop = rect_get(&lr, op);
  263.         if ( npop < 0 ) return npop;
  264.         code = gs_rectstroke(igs, lr.pr, lr.count, (gs_matrix *)0);
  265.        }
  266.     rect_release(&lr);
  267.     if ( code < 0 ) return code;
  268.     pop(npop);
  269.     return 0;
  270. }
  271.  
  272. /* --- Internal routines --- */
  273.  
  274. /* Get rectangles from the stack. */
  275. /* Return the number of elements to pop (>0) if OK, <0 if error. */
  276. private int
  277. rect_get(local_rects *plr, os_ptr op)
  278. {    int code, npop;
  279.     stream st;
  280.     uint n, count;
  281.     gs_rect *pr;
  282.     switch ( r_type(op) )
  283.        {
  284.     case t_array:
  285.     case t_string:
  286.         code = sread_num_array(&st, op);
  287.         if ( code < 0 ) return code;
  288.         count = scount_num_stream(&st);
  289.         if ( count % 4 )
  290.             return_error(e_typecheck);
  291.         count /= 4, npop = 1;
  292.         break;
  293.     default:            /* better be 4 numbers */
  294.         sread_string(&st, (byte *)(op - 3), sizeof(ref) * 4);
  295.         st.num_format = num_array;
  296.         count = 1, npop = 4;
  297.         break;
  298.        }
  299.     plr->count = count;
  300.     if ( count <= max_local_rect )
  301.         pr = plr->rl;
  302.     else
  303.        {    pr = (gs_rect *)alloc(count, sizeof(gs_rect), "rect_get");
  304.         if ( pr == 0 )
  305.             return_error(e_VMerror);
  306.        }
  307.     plr->pr = pr;
  308.     for ( n = 0; n < count; n++, pr++ )
  309.        {    ref rnum;
  310.         float rv[4];
  311.         int i;
  312.         for ( i = 0; i < 4; i++ )
  313.            {    switch ( code = sget_encoded_number(&st, &rnum) )
  314.                {
  315.             case t_integer:
  316.                 rv[i] = rnum.value.intval;
  317.                 break;
  318.             case t_real:
  319.                 rv[i] = rnum.value.realval;
  320.                 break;
  321.             default:    /* code < 0 */
  322.                 return code;
  323.                }
  324.            }
  325.         pr->q.x = (pr->p.x = rv[0]) + rv[2];
  326.         pr->q.y = (pr->p.y = rv[1]) + rv[3];
  327.        }
  328.     return npop;
  329. }
  330.  
  331. /* Release the rectangle list if needed. */
  332. private void
  333. rect_release(local_rects *plr)
  334. {    if ( plr->pr != plr->rl )
  335.         alloc_free((char *)plr->pr, plr->count, sizeof(gs_rect),
  336.                "rect_release");
  337. }
  338.  
  339. /* ------ Graphics state ------ */
  340.  
  341. /* <llx> <lly> <urx> <ury> setbbox - */
  342. int
  343. zsetbbox(register os_ptr op)
  344. {    float box[4];
  345.     int code = num_params(op, 4, box);
  346.     if ( code < 0 ) return code;
  347.     if ( (code = gs_setbbox(igs, box[0], box[1], box[2], box[3])) < 0 )
  348.         return code;
  349.     pop(4);
  350.     return 0;
  351. }
  352.  
  353. /* ------ Initialization procedure ------ */
  354.  
  355. op_def zdps1_op_defs[] = {
  356.         /* Graphics state */
  357.     {"0currenthalftonephase", zcurrenthalftonephase},
  358.     {"0currentstrokeadjust", zcurrentstrokeadjust},
  359.     {"2sethalftonephase", zsethalftonephase},
  360.     {"1setstrokeadjust", zsetstrokeadjust},
  361.         /* Graphics state objects */
  362.     {"1currentgstate", zcurrentgstate},
  363.     {"0gstate", zgstate},
  364.     {"1setgstate", zsetgstate},
  365.         /* Rectangles */
  366.     {"1rectappend", zrectappend},
  367.     {"1rectclip", zrectclip},
  368.     {"1rectfill", zrectfill},
  369.     {"1rectstroke", zrectstroke},
  370.         /* Graphics state components */
  371.     {"4setbbox", zsetbbox},
  372.     op_def_end(zdps1_init)
  373. };
  374.